//
//  ViewController2.m
//  获取设置的参数
//
//  Created by Clement_Gu on 16/3/16.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()
@property (nonatomic,strong) UISwitch *wrapSwitch;
@property (nonatomic,strong) UISlider *wrapSpeedSlider;
@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //创建switch
    UISwitch *swi = [[UISwitch alloc]initWithFrame:CGRectMake(100, 70, 100, 50)];
    [swi addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventTouchUpInside];
    self.wrapSwitch = swi;
    [self.view addSubview:swi];
    //创建slider
    UISlider *sd = [[UISlider alloc]initWithFrame:CGRectMake(40, 200, 250, 30)];
    [sd addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
    sd.minimumValue = 1;
    sd.maximumValue = 10;
    self.wrapSpeedSlider = sd;
    [self.view addSubview:sd];
    //创建返回按钮
    UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(comeBack)];
    self.navigationItem.leftBarButtonItem = btn;
    
    //更新数据
    [self updateInfo];
    
    //监听程序进入前台的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateInfo) name:UIApplicationWillEnterForegroundNotification object:nil];
    
}
//switch变化
-(void)switchChange:(UISwitch *)sender
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setBool:sender.on forKey:@"wrap"];
    [user synchronize];
}
//slider滑动
-(void)sliderChange:(UISlider *)sender
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setFloat:sender.value forKey:@"wrapSpeed"];
    [user synchronize];

}

//页面返回
-(void)comeBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//移除通知
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

//更新数据
-(void)updateInfo
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    self.wrapSwitch.on = [user boolForKey:@"wrap"];
    self.wrapSpeedSlider.value = [user floatForKey:@"wrapSpeed"];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
